package com.woniuxy.oldmanapi.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.oldmanapi.entity.Outapplication;
import com.woniuxy.oldmanapi.mapper.OutapplicationMapper;
import com.woniuxy.oldmanapi.yvo.OutApplicationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.From;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class OutapplicationService {
    @Autowired
    OutapplicationMapper outapplicationMapper;

    /**
     * 此方法为初始化外出申请表页面数据 分页展示
     *
     * @param name 搜索框条件
     * @param now  分页 页数
     * @param size 分页 条数
     * @return
     */
    public PageInfo<OutApplicationVo> initSelectAllByCons(String name, int now, int size) {
        PageHelper.startPage(now, size);
        List<OutApplicationVo> arr = outapplicationMapper.initSelectAllByCons(name);
        return new PageInfo<>(arr);
    }

    /**
     * 此方法为添加外出申请
     *
     * @param outapplication 外出申请对象 非 vo
     * @return
     */

    public void addOutApplication(Outapplication outapplication) {
        outapplication.setBackstatus(0);
        outapplicationMapper.insert(outapplication);
    }

    /**
     * 此方法用于 打开编辑修改时 查询一个对象 返显使用
     *
     * @param id 查询对象的Id
     * @return
     */

    public OutApplicationVo selectOutApplicationById(int id) {
        return outapplicationMapper.selectOutApplicationById(id);
    }

    /**
     * 此方位用于 修改一个外出申请记录
     *
     * @param outapplication
     * @return
     */
    public void modifyOutApplication(Outapplication outapplication) {
        outapplication.setBackstatus(0);
        outapplicationMapper.updateByPrimaryKey(outapplication);
    }

    /**
     * 此方法用于删除一个外出申请记录
     *
     * @param id
     * @return
     */
    public void deleteOutApplication(int id) {
        outapplicationMapper.deleteByPrimaryKey(id);
    }

    /**
     * 此方位用于 修改一个外出申请记录的状态 修改为返院 并计算外出总时间
     *
     * @param newOutapplication  前端传回,包含Id,返回时间
     * @return
     */
    public void modifyOutApplicationStatus(Outapplication newOutapplication) {
        //通过传回的Id 查到原本的申请对象
        Outapplication olderOutapplication = outapplicationMapper.selectByPrimaryKey(newOutapplication.getId());
        //拿出返回时间
        Date backdatetime = newOutapplication.getBackdatetime();
        long time = backdatetime.getTime();
        //拿出此对象的外出时间
        Date outdatetime = olderOutapplication.getOutdatetime();
        long time1 = outdatetime.getTime();
        //计算出差值
        long newTime = time - time1;
        //天
        long days = TimeUnit.MILLISECONDS.toDays(newTime);
        //时
        long hours = TimeUnit.MILLISECONDS.toHours(newTime) % 24;
        //分
        long minutes = TimeUnit.MILLISECONDS.toMinutes(newTime) % 60;
        //转成天,时,分 格式
        String dateTime = days+"天"+hours+"时"+minutes+"分";
        //给老对象set外出时间属性和状态
        olderOutapplication.setOutdays(dateTime);
        olderOutapplication.setBackstatus(1);
        //修改
        olderOutapplication.setBackdatetime(backdatetime);
        outapplicationMapper.updateByPrimaryKey(olderOutapplication);
    }
}
